package com.shruijin.wy.proxy.dynamic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * 参考文献： https://www.cnblogs.com/qlqwjy/p/7550609.html
 */

public class DynamicProxy {

    Logger  LOGGER = LoggerFactory.getLogger(this.getClass());

    private Object target;

    public DynamicProxy (Object object){
        this.target = object;
    }

    public Object getInstance(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                LOGGER.info("开始获取代理对象！");
                Object retObj = method.invoke(target,objects);
                LOGGER.info("获取代理对象结束！");
                return retObj;
            }
        });
    }
}
